<?php
namespace Admin\Controller;

use Common\Controller\AuthController;
use Think\Controller;

class StockController extends AuthController
{
    //仓库管理首页
    public function index()
    {
        $arr = D('Material')->kindsList();
        $this->assign('_list', $arr['list']);
        $this->assign('_page', $arr['page']->show());

        $this->assign('title','货品列表');
        $this->display();
    }

    //新增货品
    public function padd()
    {

        if (IS_POST) {
            if ($_POST['counts'] !== '') {
                $counts = $_POST['counts'];
            }
            unset($_POST['counts']);
            $_POST['admin_id'] = $_SESSION['tc']['uid'];
            $_POST['put_time'] = time();
            $arr = D('Material')->newAdd('', $_POST);
            if ($arr['state'] == 'success') {
                $this->success($arr['info']);
            } else {
                $this->error($arr['info']);
            }
        } else {
            $list = M('Shipping')->where("status=%d", 1)->select();
            $darr = M('Danwei')->where("status=%d", 1)->select();
            $this->assign('sping', $list);
            $this->assign('darr', $darr);
            $this->assign('title','新增货品');
            $this->display();
        }

    }

    //编辑货品信息
    public function pedit($mid = '')
    {

        if (IS_POST) {

            $arr = D('Material')->newAdd($mid, $_POST);
            if ($arr['state'] == 'success') {
                $this->success($arr['info']);
            } else {
                $this->error($arr['info']);
            }
        } else {
            $re = M('Material')->where("mid=%d", $mid)->find();
            $list = M('Shipping')->where("status=%d", 1)->select();
            $darr = M('Danwei')->where("status=%d", 1)->select();
            $this->assign('sping', $list);
            $this->assign('darr', $darr);
            $this->assign('re', $re);
            $this->assign('title','编辑货品');
            $this->display();
        }

    }

    //删除货品
    public function pdel($mid = '')
    {
        if (M('Material')->where("mid=%d", $mid)->save(['status' => -1])) {
            $this->success('删除成功', '', IS_AJAX);
        } else {
            $this->error('删除失败', '', IS_AJAX);
        }
    }

    //录入库存
    public function entering()
    {

        if (IS_POST) {
            if (empty($_POST)) {
                $this->error('请先添加货品');
                exit;
            }
            $pArray = putarry($_POST, 'counts');
            $uid = $_SESSION['tc']['uid'];
            $area_id = $_SESSION['tc']['area_id'];
            $fObj = M('Finishedgood');
            $fObj->startTrans();//开启事务
            try {
                $stockData['log_number']=getOrderNumber();
                $stockData['counts']=count($pArray);
                $stockData['create_time']=time();
                $stockData['admin_id']=$uid;
                $lid=M('Stocklog')->add($stockData);//得到进货订单id

                foreach ($pArray as $v) {
                    M('Stockproduct')->add(['lid'=>$lid,'bracode'=>$v['bracode'],'counts'=>$v['counts']]);
                    $findArr = $fObj->where("bracode='%s'", $v['bracode'])->find();
                    if ($findArr) {
                        $fObj->where("bracode='%s'", $v['bracode'])->setInc('counts', $v['counts']);
                    } else {
                        $v['admin_id'] = $uid;
                        $v['area_id'] = $area_id;
                        $fObj->add($v);
                    }

                }
                $fObj->commit();//成功则提交事务
                $this->success('录入成功');
            } catch (Exception $e) {
                $fObj->rollback();//不成功，则回滚事务
                $this->error('录入失败');
            }


        } else {
            $this->assign('title','库存录入');
            $this->display();
        }

    }

    //查询货品
    public function search($keywords = '')
    {

        $arr = D('Material')->searchKinds($keywords);
        $this->ajaxReturn($arr);

    }

    //系统货品搜索结果
    public function test()
    {

        if(IS_AJAX){
            $arr = M('Material')->field('m_name,bracode,m_number')->where("status=%d", 1)->select();
            $data = [];
            $data['message'] = '';
            $data['value'] = $arr;
            $data['code'] = 200;
            $data['redirect'] = '';
            echo json_encode($data);
        }

    }

    //库存货品搜索结果
    public function sousuo()
    {

        if(IS_AJAX){
            $area_id=$_SESSION['tc']['area_id'];
            $arr = M('Material')->alias("a")->field('a.m_name,a.bracode,a.m_number')
                ->join("sr_finishedgood as b on a.bracode=b.bracode")
                ->where("b.area_id=%d",$area_id)->select();
            $data = [];
            $data['message'] = '';
            $data['value'] = $arr;
            $data['code'] = 200;
            $data['redirect'] = '';
            echo json_encode($data);
        }


    }


    //库存列表
    public function goodList()
    {

        $area_id = $_SESSION['tc']['area_id'];
        $counts = M('Finishedgood')->where("area_id=%d", $area_id)->count();
        $page = page($counts, 20, 5);
        $list = M('Finishedgood')->alias("a")
            ->join("sr_material as b on a.bracode=b.bracode")
            ->where("a.area_id=%d", $area_id)
            ->limit($page->firstRow, $page->listRows)->select();
        $this->assign('_list', $list);
        $this->assign('_page', $page->show());
        $this->assign('title','库存信息');
        $this->display();
    }

    //进货日志列表
    public function logList()
    {

        $counts=M('Stocklog')->where("status=%d",1)->count();
        $page=page($counts,5,10);
        $list=M('Stocklog')->field("a.*,b.nickname")
            ->alias("a")->join("sr_member as b on a.admin_id=b.id")
            ->where("a.status=%d",1)->limit($page->firstRow,$page->listRows)->select();
        $this->assign('_list',$list);
        $this->assign('_page',$page->show());
        $this->assign('title','进货日志列表');
        $this->display();
    }
    //进货日志详情
    public function logDetail($lid='',$log_number='')
    {
        if(!empty($lid)){
            $counts=M('Stockproduct')->where("status=%d and lid=%d",[1,$lid])->count();
            $page=page($counts,10,5);
            $list=M('Stockproduct')->field("b.mid,b.m_name,b.bracode,b.bid_price,b.sell_price,a.counts")->alias("a")
                ->join("sr_material as b on a.bracode=b.bracode")
                ->where("a.lid=%d and a.status=%d",[$lid,1])
                ->limit($page->firstRow,$page->listRows)
                ->select();

            $totalPrices=0;
            foreach($list as $v){
                $totalPrices+=$v['bid_price'];
            }
            $this->assign('p_list',$list);
            $this->assign('counts',$counts);
            $this->assign('p_page',$page);
            $this->assign('totalPrices',$totalPrices);
            $this->assign('log_number',$log_number);
        }

        $this->display();
    }

}















